using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

using Verety.SharedMeta.DirectoryServiceMeta.Enums;

using SBPweb.Localization;
using SBPweb.Presentation.Windows.Forms.Controls;

namespace SBPweb.Modules.ContactsManager.Controls
{
	public partial class AddContactControl : ModalDockingUserControl
	{
		#region Private members

		private ContactListControl mContactListControl;
		private List<string> mGroups;
		private Regex mUserIDRegex = new Regex(@"^[a-z0-9]([a-z0-9._])*$");
		private Regex mNickNameRegex = new Regex(@"^[a-zA-Z0-9]([\w.\s_])*$");
		private Regex mGroupNameRegex = new Regex(@"^([\w\s0-9]([\w\-\s0-9])*)$");

		#endregion

		#region Constuctor logic

		public AddContactControl(ContactListControl contactListControl, List<string> groups)
			: base(contactListControl.ParentModule)
		{
			mContactListControl = contactListControl;
			mGroups = groups;
			LocalizationExtender.OnInvalidatedControls += new EventHandler(LocalizationExtender_OnInvalidatedControls);
			if (this.Parent != null)
			{
				this.Parent.Resize += new EventHandler(Parent_Resize);
			}

			this.Caption = LocalizationExtender.GetString("AddContactControl");

			InitializeComponent();

			InitializeTextboxes();
		}

		private void InitializeTextboxes()
		{
			tbServer.Text = mContactListControl.DirectoryService.Connection.Server;
			clbGroups.BeginUpdate();
			clbGroups.Items.Clear();
			foreach (string group in mGroups)
			{
				clbGroups.Items.Add(group);
			}
			clbGroups.EndUpdate();
		}

		#endregion

		private void Finalize()
		{
			this.Parent.Resize -= new EventHandler(Parent_Resize);
			LocalizationExtender.OnInvalidatedControls -= new EventHandler(LocalizationExtender_OnInvalidatedControls);
		}

		#region Event handlers

		private void LocalizationExtender_OnInvalidatedControls(object sender, EventArgs e)
		{
			this.Caption = LocalizationExtender.GetString("AddContactControl");
			Parent_Resize(sender, e);
		}

		private void Parent_Resize(object sender, EventArgs e)
		{
			this.Size = this.Parent.ClientSize;
			this.tbServer.Location = new Point(this.Width - 5 - tbServer.Width, tbServer.Location.Y);
			this.lblAT.Location = new Point(tbServer.Location.X - 5 - lblAT.Width, lblAT.Location.Y);
			this.tbUserID.Width = lblAT.Location.X - 5 - tbUserID.Left;
			this.tbNickName.Width = this.Width - 5 - tbNickName.Left;
			this.btnCancel.Location = new Point(this.Width - 5 - btnCancel.Width, this.Height - 5 - btnCancel.Height);
			this.btnSave.Location = new Point(5, this.btnCancel.Location.Y);
			this.btnAddGroup.Location = new Point(this.Width - 5 - btnAddGroup.Width, this.btnCancel.Top - 40);
			this.lblNewGroup.Location = new Point(this.tbNickName.Left, btnAddGroup.Location.Y + 5);
			this.tbNewGroup.Location = new Point(this.lblNewGroup.Location.X + 80, lblNewGroup.Location.Y - 3);
			this.tbNewGroup.Width = btnAddGroup.Left - 10 - tbNewGroup.Left;
			this.clbGroups.Location = new Point(this.tbNickName.Location.X, tbNickName.Location.Y + 26);
			this.clbGroups.Width = this.Width - 5 - clbGroups.Left;
			this.clbGroups.Height = tbNewGroup.Top - 15 - clbGroups.Top;
		}

		private void clbGroups_Validated(object sender, System.EventArgs e)
		{
			errorProvider.SetError(clbGroups, string.Empty);
			if (clbGroups.CheckedItems.Count < 1)
			{
				errorProvider.SetError(clbGroups, "Add at least to one group");
			}
		}

		private void tbUserID_TextChanged(object sender, EventArgs e)
		{
			errorProvider.SetError(tbUserID, string.Empty);
			if (tbUserID.Text.Length < 1)
			{
				errorProvider.SetError(tbUserID, LocalizationExtender.GetString("FieldValidationEmpty"));
				return;
			}
			if (tbUserID.Text.Length > IntConstants.MaxJIDLength)
			{
				errorProvider.SetError(tbUserID, LocalizationExtender.GetString("FieldValidationTooLong"));
				return;
			}
			if (!mUserIDRegex.IsMatch(tbUserID.Text))
			{
				errorProvider.SetError(tbUserID, LocalizationExtender.GetString("FieldValidationInvalidCharacter"));
				return;
			}
		}

		private void tbNickName_TextChanged(object sender, EventArgs e)
		{
			errorProvider.SetError(tbNickName, string.Empty);
			if (tbNickName.Text.Length < 1)
			{
				errorProvider.SetError(tbNickName, LocalizationExtender.GetString("FieldValidationEmpty"));
				return;
			}
			if (tbNickName.Text.Length > IntConstants.MaxNickNameLength)
			{
				errorProvider.SetError(tbNickName, LocalizationExtender.GetString("FieldValidationTooLong"));
				return;
			}
			if (!mNickNameRegex.IsMatch(tbNickName.Text))
			{
				errorProvider.SetError(tbNickName, LocalizationExtender.GetString("FieldValidationInvalidCharacter"));
				return;
			}
		}

		private void tbNewGroup_TextChanged(object sender, EventArgs e)
		{
			errorProvider.SetError(tbNewGroup, string.Empty);
			if (tbNewGroup.Text.Length < 1)
			{
				errorProvider.SetError(tbNewGroup, LocalizationExtender.GetString("FieldValidationEmpty"));
				return;
			}
			if (tbNewGroup.Text.Length > IntConstants.MaxGroupNameLength)
			{
				errorProvider.SetError(tbNewGroup, LocalizationExtender.GetString("FieldValidationTooLong"));
				return;
			}
			if (!mGroupNameRegex.IsMatch(tbNewGroup.Text))
			{
				errorProvider.SetError(tbNewGroup, LocalizationExtender.GetString("FieldValidationInvalidCharacter"));
				return;
			}
		}

		private void btnAddGroup_Click(object sender, EventArgs e)
		{
			tbNewGroup.Text = tbNewGroup.Text.Trim();
			tbNewGroup_TextChanged(sender, e);
			if (errorProvider.GetError(tbNewGroup) != string.Empty)
			{
				return;
			}
			if (!mGroups.Contains(tbNewGroup.Text))
			{
				mGroups.Add(tbNewGroup.Text);
				clbGroups.Items.Add(tbNewGroup.Text, true);
			}
			else
			{
				clbGroups.SetItemChecked(clbGroups.Items.IndexOf(tbNewGroup.Text), true);
			}
			errorProvider.SetError(clbGroups, string.Empty);
			tbNewGroup.Text = string.Empty;
			errorProvider.SetError(tbNewGroup, string.Empty);
		}

		private void btnSave_Click(object sender, EventArgs e)
		{
			clbGroups_Validated(sender, e);
			tbUserID_TextChanged(sender, e);
			tbNickName_TextChanged(sender, e);

			if (errorProvider.GetError(clbGroups) != string.Empty
				||
				errorProvider.GetError(tbUserID) != string.Empty
				||
				errorProvider.GetError(tbNickName) != string.Empty)
			{
				return;
			}

			List<string> groups = new List<string>();
			foreach (string item in clbGroups.CheckedItems)
			{
				groups.Add(item);
			}

			mContactListControl.DirectoryService.Contacts.AddContact(tbUserID.Text + "@" + tbServer.Text,
				tbNickName.Text,
				groups,
				false);

			Finalize();
			ParentModule.Host.Smartparts.Remove(this);
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			Finalize();
			ParentModule.Host.Smartparts.Remove(this);
		}

		#endregion
	}
}
